<?php

class Application_Model_DbTable_Links extends Zend_Db_Table_Abstract
{

    protected $_name = 'ojlinks';

    public function addLink(Application_Model_Link $link){
        $data = array(
            'ojliName' => $link->getName(),
            'ojliUrl' => $link->getUrl(),
            'ojliImage' => $link->getImage(),
            'ojliOpenType' => $link->getOpenType(),
            'ojliMemo' => $link->getMemo()
        );
        $this->insert($data);
        return $this;
    }
    
    public function updateLink(Application_Model_Link $link){
        $data = array(
            'ojliName' => $link->getName(),
            'ojliUrl' => $link->getUrl(),
            'ojliImage' => $link->getImage(),
            'ojliOpenType' => $link->getOpenType(),
            'ojliMemo' => $link->getMemo()
        );
        $this->update($data, 'ojliId = '.$link->getId());
        return $this;
    }
    
    public function deleteLink(int $id){
        $this->delete('ojliId = '.$id);
        return $this;
    }
    
    public function getLinkById(int $id){
        $row = $this->fetchRow('ojliId = '.$id);
        if(!$row)
            return null;
        return new Application_Model_Link($id,
                $row->ojliName,$row->ojliUrl,$row->ojliImage,$row->ojliOpenType,$row->ojliMemo);
    }
    
    public function getLinks(){
        $rowset = $this->fetchAll();
        $links = array();
        foreach ($rowset as $row) {
            $link = new Application_Model_Link($row->ojliId,
                $row->ojliName,$row->ojliUrl,$row->ojliImage,$row->ojliOpenType,$row->ojliMemo);
            $links[] = $link;
        }
        return $links;
    }

}

